package com.bignerdranch.android.criminalintent;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.Button;

import java.util.Date;

/**
 * Created by brandonc418 on 9/26/13.
 */
public class DateOrTimeFragment extends DialogFragment {
    private Date mDate;
    private Button mDateButton, mTimeButton;
    private static final String DIALOG_DATE = "date";
    private static final String DIALOG_TIME = "time";

    public static DateOrTimeFragment newInstance(Date date){
        Bundle args = new Bundle();
        args.putSerializable(DatePickerFragment.EXTRA_DATE, date);

        DateOrTimeFragment fragment = new DateOrTimeFragment();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDate = (Date) getArguments().getSerializable(DatePickerFragment.EXTRA_DATE);

        View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_date_or_time, null);

        mDateButton = (Button) v.findViewById(R.id.pick_date_button);
        mDateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // load the DatePickerFragment.
                FragmentManager fm = getActivity().getSupportFragmentManager();
                DatePickerFragment dialog = DatePickerFragment.newInstance(mDate);
                dialog.setTargetFragment(getTargetFragment(), CrimeFragment.REQUEST_DATE);
                dialog.show(fm, DIALOG_DATE);
                DateOrTimeFragment.this.dismiss();//?
            }
        });

        mTimeButton = (Button) v.findViewById(R.id.pick_time_button);
        mTimeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // load the TimePickerFragment.
                FragmentManager fm = getActivity().getSupportFragmentManager();
                TimePickerFragment dialog = TimePickerFragment.newInstance(mDate);
                dialog.setTargetFragment(getTargetFragment(), CrimeFragment.REQUEST_TIME);
                dialog.show(fm, DIALOG_TIME);
                DateOrTimeFragment.this.dismiss();
            }
        });

        return new AlertDialog.Builder(getActivity())
                .setTitle(R.string.date_or_time)
                .setView(v)
                .setNegativeButton(android.R.string.cancel, null)
                .create();
    }
}
